PHP Class/Object Functions

    PHP supports some functions that allow you to get the information about the objects and the classes. You can gain information like which object belongs to which class and which are its corresponding methods and properties. These functions come with the core PHP and no need to make any configuration directives changes in the php.ini file. There are a number of functions that you can use while working with the objects and the classes.

    1. call_user_method_array()

    This function will call the user method which is provided with the array of the parameters.

    2. call_user_method()

    This function will allow you to call the user-specified method for a specific object.

    3. class_exists()

    This function will allow you to check for the existence of the given class, this function will return the true value for the defined class else will return true.

    Syntax-

    class_exists ( $class_name [,$autoload] );

    The second parameter is optional which will specify if to call autoload or not.

    Example-

    <?php
       if (class_exists('Hello')) {
          $var = new HelloWorld();
       }
    ?>

    4. get_clas_methods()

    This function will provide you with the method names of the specified class. It will return the method names array for the defined class name else it will return false.

    Syntax-

    get_method ( $class_name );

    Example-

    <?php
       class Demo{
          function Demo() {
             return(true);
          }
          function func1() {
             return(true);
          }
          function func2() {
             return(true);
          }
       }
       $method_list = get_method(Demo);
       $method_list = get_method(new Demo());   
       foreach ($method_list as $method_name) {
          echo "$method_name \n";
       }
    ?>

    Output-

    Demo
    func1
    func2

    5. get_class_vars()

    This function will provide you with the default class properties for the given class name that is passed within the function.

    Syntax-

    get_class_vars ( $class_name );

    Example-

    <?php
       class demo_class{
          var $var1;
          var $var2 = "abc";
          private $var3;      
          function demo() {
             $this->var1 = "hello";
             $this->var2 = "bye";
             return true;
          }
       }
       $var4 = new demo_class();
       $vars = get_class_vars(get_class($var4));
       foreach ($vars as $name => $value) {
          echo "$name : $value \n";
       }
    ?>

    Output-

    var1 :
    var2 : abc

    6. get_class()

    This function will provide you with the class name for the given object name.

    Syntax-

    get_class ( $object );

    Example-

    <?php
       class demo {
          function demo() {
          }
          function demo_name() {
             echo "Hello " , get_class($this) , "\n";
          }
       }
       $b = new demo();
       echo "Bye " , get_class($b) , "\n";
       $b->demo_name();
    ?>

    Output-

    Bye demo Hello demo

    7. get_declared_classes()

    This function will return the declared classes name array that are defined within the script. This function will not take any parameter as by default this function will consider the current script to find the number of declared classes.

    Syntax-

    get_declared_classes ( void );

    Example-

    <?php
       print_r(get_declared_classes());
    ?>

    Output-

    Array (
       [0] => stdClass
    …
    )

    8. get_declared_interfaces()

    This function will provide you with the names of the declared interfaces within the script. There is no mandate parameter to be passed as by default this function will consider the current script.

    Syntax-

    get_declared_interfaces ( void );

    Example-

    <?php
       print_r(get_declared_interfaces());
    ?>

    Output-

    Array ( 
       [0] => Traversable
       [1] => IteratorAggregate
    …
    )

    9. get_object_vars()

    This function will provide you with the defined properties for the given object. This function will take up the object name for the parameter.

    Syntax-

    get_object_vars ( $object);

    Example-

     class Demo {
          var $var1, $var2;
          var $label;     
          function Demo($var1, $var2) {
             $this->xx = $var1;
             $this->yy = $var2;
          }
          function setLabel($label) {
             $this->label = $label;
          }
          function getPoint() {
             return array("xx" => $this->xx, "yy" => $this->yy, "label" => $this->label);
          }
       }
       $res = new Demo(1.233, 3.445);
       print_r(get_object_vars($res));
    ?>

    Output-

    Array ( [var1] => [var2] => [label] => [xx] => 1.233 [yy] => 3.445 )

    10. get_parent_class()

    This function will provide you with the parent class name for the given class or the object.

    Syntax-

    get_parent_class ( $object );

    Example-

    <?php
       class demo {
          function func() {
          }
       }
       class child extends demo {
          function child() {
             echo get_parent_class($this) ;
          }
       }
    $var = new child();
    ?>

    Output-

    demo

    11. interface_exists()

    This function will return true if the given interface name exists else it will return false. This function takes the second parameter which is not necessary but specifies if you want to call autoload or not.

    Syntax-

    interface_exists ( $interface_name [, $autoload] );

    Example-

    <?php
       if (interface_exists('SomeInterface')) {
          class MyClass implements SomeInterface {
          }
       }
    ?>

    12. is_a()

    This function allows you to check if the given class belongs to the given object or the object has the given class as one of its parents class.

    Syntax-

    is_a ( $object, $class_name )

    Example-

    <?php
       if ($wid_fact instanceof WidgetFactory) {
          echo $wid_fact;
       }
    ?>

    Output-

    $WF

    13. is_subclass_of()

    This function will allow you to check if the given object has the class_name as one of its parent class.

    Syntax-

    is_subclass_of ( $object, $class_name );

    Example-

    <?php
       class demo {
          var $oink = 'moo';
       }
       class demo_child extends demo {
          var $oink = 'oink';
       }
       $obj = new demo();
       $ob = new demo_child();
       if (is_subclass_of($ob, 'demo')) {
          echo "yes it is a subclass of demo \n";
       }else {
          echo "no it is not a subclass of demo \n";
       }
       ?>

    Output-

    yes it is a subclass of demo

    14. method_exists()

    This function will allow you to check if the method name exists for the given object name.

    Syntax-

    method_exists ( $object, $method_name );

    Example-

    <?php
       $directory = new Directory('.');
       var_dump(method_exists($directory,'anything'));
    ?>

    Output-

    bool(false)

    15. property_exists()

    This function will allow you to check if the given property exists for the given object name and has an accessible scope.

    Syntax-

    property_exists ( $object, $property );

    Example-

    <?php
       class demo{
          public $var;
          public f1() { echo($var); }
       }
       property_exists(demo, var);   
    ?>

    People are also reading: